﻿using System;
using System.Collections.Generic;

namespace ConditionTreeTestStand.Nodes
{
  public abstract class SecondOperandViewModel : OtherOperandsVievModel
  {

  }
  public class SecondOperandViewModel<T> : SecondOperandViewModel, ISingleSecondOperand
  {
    public Type GetValueType()
    {
      return typeof(T);
    }

    public NodeViewModel Owner { get; private set; }

    public SecondOperandViewModel(NodeViewModel owner)
    {
      Owner = owner;
    }

    #region ' SecondOperand Property (change notification) '

    public T SecondOperand
    {
      get { return _secondOperand; }
      set
      {
        if (Equals(_secondOperand, value)) return;
        _secondOperand = value;
        OnSecondOperandChanged();
        NotifyOfPropertyChange(() => SecondOperand);
      }
    }

    private T _secondOperand;

    #endregion

    protected virtual void OnSecondOperandChanged()
    {
      Owner.OnConditionChanged();
    }

    public override bool IsEntered
    {
      get { return SecondOperand != null; }
    }

    public override IEnumerable<object> GetValues()
    {
      yield return SecondOperand;
    }
    public override string ToString()
    {
      return SecondOperand == null ? "" : SecondOperand.ToString();
    }

    public object Value
    {
      get { return SecondOperand; }
      set { SecondOperand = (T)value; }
    }

    public virtual string StrValue
    {
      get { return Value == null ? null : Value.ToString(); }
      set
      {
        Value = string.IsNullOrWhiteSpace(value)
                  ? default(T)
                  : Convert.ChangeType(value, typeof (T));
      }
    }
  }
}